package com.dxy.df.utils;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author admin
 * @Description:
 * @date 2018/5/15 下午5:25
 */
public class RequestUtil {


    public static void setCookie(HttpServletRequest request, HttpServletResponse response,
                                 String cookieName, String cookieValue, int expiredSeconds, String contextPath) {
        Cookie cookie = new Cookie(cookieName, cookieValue);
        //设置Maximum Age
        cookie.setMaxAge(expiredSeconds);
        //设置cookie路径为当前项目路径
        cookie.setPath(contextPath);
        //添加cookie
        cookie.setHttpOnly(true);

        response.addCookie(cookie);
    }

    public static void setCookie(HttpServletRequest request, HttpServletResponse response,
                                 String cookieName, String cookieValue, int expiredSeconds) {
        Cookie cookie = new Cookie(cookieName, cookieValue);
        //设置Maximum Age
        cookie.setMaxAge(expiredSeconds);
        //设置cookie路径为当前项目路径
        cookie.setPath(request.getContextPath());
        //添加cookie
        cookie.setHttpOnly(true);

        response.addCookie(cookie);
    }


    public static Map<String, Cookie> getCookieMap(HttpServletRequest request) {
        Map<String, Cookie> cookieMap = new HashMap<>();
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }

    public static String getCookieValue(Map<String, Cookie> map, String key) {
        Iterator<Map.Entry<String, Cookie>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, Cookie> entry = it.next();
            if (entry.getKey().equals(key)) {
                return entry.getValue().getValue();
            }
        }
        return null;
    }


    public static String getCookieValue(HttpServletRequest request, String key) {
        Map<String, Cookie> cookieMap = getCookieMap(request);
        return getCookieValue(cookieMap,key);
    }

   /* public static boolean removeCookie(HttpServletRequest request, HttpServletResponse response,
                                       String name) {
        if (StringUtil.isBlank(name)) {
            return false;
        }
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                    return true;
                }
            }
        }
        return false;
    }*/
/*
    public static boolean removeCookie(HttpServletRequest request, HttpServletResponse response,
                                       String name, String path) {
        if (StringUtil.isBlank(name)) {
            return false;
        }
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    cookie.setValue(null);
                    cookie.setMaxAge(0);
                    cookie.setPath(path);
                    response.addCookie(cookie);
                    return true;
                }
            }
        }
        return false;
    }*/

}
